<template>
  <div class="inquiry-records">
    <!-- 搜索和筛选 -->
    <el-card class="filter-card">
      <el-form :model="searchForm" class="filter-form" :inline="true">
        <el-form-item label="项目编号/名称">
          <el-input
            v-model="keyword"
            placeholder="请输入项目编号或名称"
            prefix-icon="el-icon-search"
            style="width: 200px;"
          ></el-input>
        </el-form-item>
        
        <el-form-item label="状态">
          <el-select
            v-model="search.status"
            placeholder="请选择状态"
            style="width: 150px;"
          >
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.text"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="开始日期">
          <el-date-picker
            v-model="search.startDate"
            type="date"
            placeholder="选择开始日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            :picker-options="datePickerOptions"
            style="width: 150px;"
          ></el-date-picker>
        </el-form-item>
        
        <el-form-item label="结束日期">
          <el-date-picker
            v-model="search.endDate"
            type="date"
            placeholder="选择结束日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            :picker-options="datePickerOptions"
            style="width: 150px;"
          ></el-date-picker>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="fetchData" icon="el-icon-search">
            查询
          </el-button>
          <el-button @click="handleReset" icon="el-icon-refresh">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 数据表格 -->
    <el-card class="table-card">
      <el-table
        :data="inquiries"
        v-loading="loading"
        class="inquiry-table"
        stripe
        border
      >
        <el-table-column
          prop="InquiryNo"
          label="询标编号"
          width="150"
        ></el-table-column>
        
        <el-table-column
          prop="ProjectNo"
          label="项目编号"
          width="150"
        ></el-table-column>
        
        <el-table-column
          prop="ProjectName"
          label="项目名称"
          min-width="200"
          show-overflow-tooltip
        ></el-table-column>
        
        <el-table-column
          label="状态"
          width="120"
        >
          <template slot-scope="scope">
            <el-tag
              :type="getStatusType(scope.row)"
              size="small"
            >
              {{ getStatusText(scope.row) }}
            </el-tag>
          </template>
        </el-table-column>
        
        <el-table-column
          label="创建时间"
          width="180"
        >
          <template slot-scope="scope">
            {{ formatDate(scope.row.CreateTime) }}
          </template>
        </el-table-column>
        
        <el-table-column
          label="操作"
          width="120"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="viewDetail(scope.row)"
            >
              查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页 -->
      <div class="pagination-wrapper">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="itemsPerPage"
          :total="totalItems"
          layout="total, sizes, prev, pager, next, jumper"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { GetInquiryReplyRecordsAdmin } from '@/api/BidderInquiry'

export default {
  name: 'InquiryRecords',
  data() {
    return {
      loading: false,
      page: 1,
      itemsPerPage: 10,
      totalItems: 0,
      keyword: '',
      search: {
        status: '',
        startDate: '',
        endDate: ''
      },
      searchForm: {},
      inquiries: [],
      // 日期选择器配置
      datePickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      },
      statusOptions: [
        { text: '全部', value: '' },
        { text: '待回复', value: 'pending' },
        { text: '已回复', value: 'replied' },
        { text: '已完成', value: 'completed' }
      ]
    }
  },
  mounted() {
    const today = this.getToday()
    this.search.startDate = today.substring(0, 7) + '-01'
    this.search.endDate = today
    this.fetchData()
  },
  methods: {
    getToday() {
      const today = new Date()
      const yyyy = today.getFullYear()
      const mm = String(today.getMonth() + 1).padStart(2, '0')
      const dd = String(today.getDate()).padStart(2, '0')
      return `${yyyy}-${mm}-${dd}`
    },
    getStatusType(item) {
      if (new Date(item.DeadlineTime) < new Date() && item.IsReply === false) {
        return 'danger'
      }
      return item.IsReply ? 'success' : 'warning'
    },
    getStatusText(item) {
      if (new Date(item.DeadlineTime) < new Date() && item.IsReply === false) {
        return '已截止'
      }
      return item.IsReply ? '已回复' : '待回复'
    },
    formatDate(date) {
      return new Date(date).toLocaleString()
    },
    viewDetail(item) {
      this.$router.push({ 
        name: 'expert-inquiry-detail',
        query: {
          Id: item.Id,
          timestamp: new Date().getTime()
        }
      })
    },
    async fetchData() {
      this.loading = true
      try {
        const params = {
          pageIndex: this.page,
          pageSize: this.itemsPerPage,
          keyword: this.keyword,
          start: this.search.startDate,
          end: this.search.endDate,
        }
        const res = await GetInquiryReplyRecordsAdmin(params)
        this.inquiries = res.Data.Data || []
        this.totalItems = res.Data.Count || 0
      } catch (error) {
        console.error('获取询标记录失败:', error)
        this.$message.error('获取询标记录失败')
      } finally {
        this.loading = false
      }
    },
    
    // 重置搜索条件
    handleReset() {
      const today = this.getToday()
      this.keyword = ''
      this.search = {
        status: '',
        startDate: today.substring(0, 7) + '-01',
        endDate: today
      }
      this.page = 1
      this.fetchData()
    },
    
    // 分页大小改变
    handleSizeChange(val) {
      this.itemsPerPage = val
      this.page = 1
      this.fetchData()
    },
    
    // 当前页改变
    handleCurrentChange(val) {
      this.page = val
      this.fetchData()
    }
  }
};
</script>

<style scoped>
.inquiry-records {
  padding: 20px;
}

.filter-card {
  margin-bottom: 20px;
}

.filter-form {
  padding: 20px;
}

.filter-form .el-form-item {
  margin-bottom: 18px;
}

.filter-form .el-button {
  margin-left: 10px;
}

.table-card {
  margin-bottom: 20px;
}

.table-card .el-card__body {
  padding: 0;
}

.inquiry-table {
  width: 100%;
}

.inquiry-table .el-table__header {
  background-color: #f5f7fa;
}

.inquiry-table .el-table th {
  background-color: #f5f7fa;
  color: #606266;
  font-weight: 600;
}

.inquiry-table .el-table td {
  padding: 12px 0;
}

.pagination-wrapper {
  text-align: right;
  padding: 20px;
  background-color: #fff;
  border-top: 1px solid #ebeef5;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .inquiry-records {
    padding: 10px;
  }
  
  .filter-form {
    padding: 15px;
  }
  
  .filter-form .el-form-item {
    margin-bottom: 15px;
  }
  
  .filter-form .el-form-item .el-input,
  .filter-form .el-form-item .el-select,
  .filter-form .el-form-item .el-date-picker {
    width: 100% !important;
  }
  
  .pagination-wrapper {
    text-align: center;
    padding: 15px;
  }
  
  .inquiry-table .el-table td {
    padding: 8px 0;
    font-size: 12px;
  }
}
</style>